<?php

class Pages extends Controller {

	function Pages()
	{
		parent::Controller();
		$this->load->helper('admin');
		
		construct_admin_controller($this->EzAuth_Model);
		
		
	}
	
	
	//do something before we load the page:
	//check to see if you are logged in
	
	function _remap($method) {
		checkauth($this, $method, $this->EzAuth_Model);
	}
	

	
	function index()
	{
		$offset = $this->uri->segment(4);
		$data = get_page_ready('pages',$offset, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		$config['base_url'] = base_url() . 'index.php/admin/pages/page/';
		$config['total_rows'] = $data['numpages'];
		$config['uri_segment'] = 4;
		$config['per_page'] = '10';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		//make the dates pretty
		$this->load->helper('date');
		if (!empty($data['pages'])) { foreach ($data['pages'] as $key =>$page) {
			$newdate = relative_time($page->date);
			$data['pages'][$key]->date = $newdate;
		}} else { $data['pages'] = ''; }
		
		$data['location'] = "pages";
		$this->load->view('admin/pages', $data);
	}
	
	function page() {
		$this->index();
	}
	
	function process() {
		//delete, make visible or make invisible?
		if(!empty($_POST['button'])) {
			switch  ($_POST['button']) {
				case "Delete":
					if (!empty($_POST['checkall']))
						$this->delete($_POST['checkall']);
					else
						$this->index();
					break;
				case "Make Invisible":
					if (!empty($_POST['checkall']))
						$this->makeinvisible($_POST['checkall']);
					$this->index();
					break;
				case "Make Visible":
					if (!empty($_POST['checkall']))
						$this->makevisible($_POST['checkall']);
					$this->index();
					break;
					break;
				default:
					$this->index();
					break;
			}
		} else {
			$this->index();
		}
	}
	
	function delete($pages) {
		//pages to delete are in $_POST
		
		//are you sure?
		$data = get_page_ready('areyousure',0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		foreach ($pages as $pageindex => $page) {
			//get the title of the page
			$pages[$pageindex] = $this->Pages_Model->get_title($page);
		}
		
		$data['pages'] = $pages;
		$data['offset'] = $_POST['offset'];
		$this->load->view('admin/areyousure', $data);
		
	}
	
	function yesiamsure() {
		//really delete the pages
		//the pages to delete are in an array called $_POST['pages']
		foreach ($_POST['pages'] as $page) {
			$this->db->or_where('title', $page);
		}
		$this->db->delete('pages');
		$this->index();
		
	}
	
	function makeinvisible($pages) {
		foreach ($pages as $page) {
			//echo "invisibled page number " . $page . "<br />";
			$this->db->or_where('id', $page);
		}
		$this->db->update('pages', array('invisible' => 1));
	}
	
	function makevisible($pages) {
		foreach ($pages as $page) {
			//echo "invisibled page number " . $page . "<br />";
			$this->db->or_where('id', $page);
		}
		$this->db->update('pages', array('invisible' => 0));
	}
	
	function search() {
		print_r($_POST);
		die("searched!");
	}
	

}
?>